package logic

import (
	"context"
	"doctor-medicine-system/model/mysql"
	"doctor-medicine-system/usersrpc/internal/svc"
	"doctor-medicine-system/usersrpc/usersrpc"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type MedicineAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMedicineAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MedicineAddLogic {
	return &MedicineAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MedicineAddLogic) MedicineAdd(in *usersrpc.MedicineAddRequest) (*usersrpc.MedicineAddResponse, error) {
	// todo: add your logic here and delete this line
	var FindDoc mysql.Doctor

	if err := FindDoc.FindDocById(mysql.DB, in.DocId); err != nil {
		return nil, err
	}
	if FindDoc.ID == 0 {
		return nil, errors.New("医生不存在")
	}
	medicine := &mysql.Medicine{
		Model:    gorm.Model{},
		Name:     in.Name,
		Content:  in.Content,
		From:     in.From,
		Message:  in.Message,
		Sign:     in.Sign,
		DoctorId: int(in.DocId),
	}
	err := medicine.MedicineAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("添加失败")
	}
	return &usersrpc.MedicineAddResponse{
		MedicineId: int64(medicine.ID),
	}, nil
}
